package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.DailyReportBean;

/**
 * 日报列表适配器
 */
public class DailyReportAdapter extends AppAdapter<DailyReportBean> {

    private static final int TYPE_DAILY_REPORT = 0; // 日报
    private static final int TYPE_VISIT = 1;        // 拜访

    private OnCommentClickListener mCommentClickListener;

    public DailyReportAdapter(Context context) {
        super(context);
    }

    /**
     * 设置评论点击监听
     */
    public void setOnCommentClickListener(OnCommentClickListener listener) {
        mCommentClickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        DailyReportBean item = getItem(position);
        if ("拜访".equals(item.getType())) {
            return TYPE_VISIT;
        }
        return TYPE_DAILY_REPORT;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_VISIT) {
            return new VisitViewHolder();
        } else {
            return new DailyReportViewHolder();
        }
    }

    /**
     * 日报 ViewHolder
     */
    private final class DailyReportViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView mAvatarView;
        private final TextView mNameView;
        private final TextView mTimeView;
        private final TextView mTypeLabelView;
        
        // 8个内容TextView
        private final TextView mContent1View;
        private final TextView mContent2View;
        private final TextView mContent3View;
        private final TextView mContent4View;
        private final TextView mContent5View;
        private final TextView mContent6View;
        private final TextView mContent7View;
        private final TextView mContent8View;
        
        private final TextView mPhotoAuthorView;
        private final RecyclerView mImagesRecyclerView;
        private final ImageGridAdapter mImageAdapter;
        
        private final TextView mCommentBtnView;
        private final RecyclerView mCommentsRecyclerView;
        private final CommentAdapter mCommentAdapter;

        private DailyReportViewHolder() {
            super(R.layout.daily_report_item);
            
            mAvatarView = findViewById(R.id.iv_avatar);
            mNameView = findViewById(R.id.tv_name);
            mTimeView = findViewById(R.id.tv_time);
            mTypeLabelView = findViewById(R.id.tv_type_label);
            
            mContent1View = findViewById(R.id.tv_content_1);
            mContent2View = findViewById(R.id.tv_content_2);
            mContent3View = findViewById(R.id.tv_content_3);
            mContent4View = findViewById(R.id.tv_content_4);
            mContent5View = findViewById(R.id.tv_content_5);
            mContent6View = findViewById(R.id.tv_content_6);
            mContent7View = findViewById(R.id.tv_content_7);
            mContent8View = findViewById(R.id.tv_content_8);
            
            mPhotoAuthorView = findViewById(R.id.tv_photo_author);
            mImagesRecyclerView = findViewById(R.id.rv_images);
            mCommentBtnView = findViewById(R.id.tv_comment_btn);
            mCommentsRecyclerView = findViewById(R.id.rv_comments);

            // 设置图片网格（每行3列）
            mImagesRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
            mImageAdapter = new ImageGridAdapter(getContext());
            mImagesRecyclerView.setAdapter(mImageAdapter);

            // 设置评论列表
            mCommentsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mCommentAdapter = new CommentAdapter(getContext());
            mCommentsRecyclerView.setAdapter(mCommentAdapter);

            // 设置评论按钮点击
            mCommentBtnView.setOnClickListener(v -> {
                int position = getViewHolderPosition();
                if (mCommentClickListener != null) {
                    DailyReportBean item = getItem(position);
                    mCommentClickListener.onCommentClick(position, item);
                }
                
                // 切换评论列表显示/隐藏
                if (mCommentsRecyclerView.getVisibility() == View.VISIBLE) {
                    mCommentsRecyclerView.setVisibility(View.GONE);
                } else {
                    mCommentsRecyclerView.setVisibility(View.VISIBLE);
                }
            });
        }

        @Override
        public void onBindView(int position) {
            DailyReportBean item = getItem(position);
            
            // 显示基本信息
            mNameView.setText(item.getName());
            mTimeView.setText("2025-01-01 10:00");
            mTypeLabelView.setText(item.getType());
            
            // 设置图片作者
            mPhotoAuthorView.setText("@" + item.getName());
            
            // 加载图片列表
            if (item.getImageList() != null && !item.getImageList().isEmpty()) {
                mImagesRecyclerView.setVisibility(View.VISIBLE);
                mImageAdapter.setData(item.getImageList());
            } else {
                mImagesRecyclerView.setVisibility(View.GONE);
            }
            
            // 加载评论列表
            if (item.getCommentList() != null && !item.getCommentList().isEmpty()) {
                mCommentAdapter.setData(item.getCommentList());
                // 如果有评论，默认显示评论列表
                mCommentsRecyclerView.setVisibility(View.VISIBLE);
            } else {
                mCommentsRecyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 拜访 ViewHolder
     */
    private final class VisitViewHolder extends AppAdapter<?>.ViewHolder {

        private final ImageView mAvatarView;
        private final TextView mNameView;
        private final TextView mTimeView;
        private final TextView mTypeLabelView;
        private final TextView mVisitTitleView;
        private final TextView mTerminalView;
        private final TextView mVisitDateView;
        private final TextView mStartTimeView;
        private final TextView mEndTimeView;
        private final TextView mDurationView;
        private final TextView mPhotoAuthorView;
        private final RecyclerView mImagesRecyclerView;
        private final ImageGridAdapter mImageAdapter;
        private final TextView mCommentBtnView;
        private final RecyclerView mCommentsRecyclerView;
        private final CommentAdapter mCommentAdapter;

        private VisitViewHolder() {
            super(R.layout.visit_report_item);
            
            mAvatarView = findViewById(R.id.iv_avatar);
            mNameView = findViewById(R.id.tv_name);
            mTimeView = findViewById(R.id.tv_time);
            mTypeLabelView = findViewById(R.id.tv_type_label);
            mVisitTitleView = findViewById(R.id.tv_visit_title);
            mTerminalView = findViewById(R.id.tv_terminal);
            mVisitDateView = findViewById(R.id.tv_visit_date);
            mStartTimeView = findViewById(R.id.tv_start_time);
            mEndTimeView = findViewById(R.id.tv_end_time);
            mDurationView = findViewById(R.id.tv_duration);
            mPhotoAuthorView = findViewById(R.id.tv_photo_author);
            mImagesRecyclerView = findViewById(R.id.rv_images);
            mCommentBtnView = findViewById(R.id.tv_comment_btn);
            mCommentsRecyclerView = findViewById(R.id.rv_comments);

            // 设置图片网格（每行3列）
            mImagesRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
            mImageAdapter = new ImageGridAdapter(getContext());
            mImagesRecyclerView.setAdapter(mImageAdapter);

            // 设置评论列表
            mCommentsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mCommentAdapter = new CommentAdapter(getContext());
            mCommentsRecyclerView.setAdapter(mCommentAdapter);

            // 设置评论按钮点击
            mCommentBtnView.setOnClickListener(v -> {
                int position = getViewHolderPosition();
                if (mCommentClickListener != null) {
                    DailyReportBean item = getItem(position);
                    mCommentClickListener.onCommentClick(position, item);
                }
                
                // 切换评论列表显示/隐藏
                if (mCommentsRecyclerView.getVisibility() == View.VISIBLE) {
                    mCommentsRecyclerView.setVisibility(View.GONE);
                } else {
                    mCommentsRecyclerView.setVisibility(View.VISIBLE);
                }
            });
        }

        @Override
        public void onBindView(int position) {
            DailyReportBean item = getItem(position);
            
            // 显示基本信息
            mNameView.setText(item.getName());
            mTimeView.setText("2025-01-01 10:00");
            mTypeLabelView.setText(item.getType());
            
            // 显示拜访信息
            mVisitTitleView.setText(item.getTitle());
            mTerminalView.setText("终端：生鲜店");
            mVisitDateView.setText("拜访日期：2025-08-11");
            mStartTimeView.setText("开始时间：12:34  距离客户位置小于1000.0米");
            mEndTimeView.setText("离开时间：12:34  距离客户位置小于1000.0米");
            mDurationView.setText("在该时长：1小时");
            
            // 设置图片作者
            mPhotoAuthorView.setText("@" + item.getName());
            
            // 加载图片列表
            if (item.getImageList() != null && !item.getImageList().isEmpty()) {
                mImagesRecyclerView.setVisibility(View.VISIBLE);
                mImageAdapter.setData(item.getImageList());
            } else {
                mImagesRecyclerView.setVisibility(View.GONE);
            }
            
            // 加载评论列表
            if (item.getCommentList() != null && !item.getCommentList().isEmpty()) {
                mCommentAdapter.setData(item.getCommentList());
                // 如果有评论，默认显示评论列表
                mCommentsRecyclerView.setVisibility(View.VISIBLE);
            } else {
                mCommentsRecyclerView.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 评论点击监听接口
     */
    public interface OnCommentClickListener {
        void onCommentClick(int position, DailyReportBean item);
    }
}
